package com.gemme.gemme.shequ;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.fragment.BaseLazyFragment;
import com.gemme.gemme.home.BrandBean;
import com.gemme.gemme.home.ProductListActivity;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.SingleBrandView;
import com.gemme.gemme.view.StaggeredDividerItemDecoration;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by sp on 2019/7/18.
 * 首页
 */
public class BrandNewFragment extends BaseLazyFragment  {

    View inflate;
    Context mContext;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        inflate = inflater.inflate(R.layout.fragment_brand_new, container, false);

        mContext = BrandNewFragment.this.getContext();
        initView(inflate);

        return inflate;
    }


    private SwipeRecyclerView mRecyclerView;
    private List<BrandBean.DataBean> mDataList = new ArrayList<>();
    StaggeredDividerItemDecoration staggeredDividerItemDecoration;
    private void initView(View inflate) {
        mRecyclerView = inflate.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));


      getData();

    }

    private void getData() {
        String url = DataManager.BaseUrl_panghu + "mall/shopbrandinfo/brandInfoList";
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer(mContext, url, true, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    BrandBean brandBean = JSON.parseObject(response, BrandBean.class);
                    List<BrandBean.DataBean> data = brandBean.getData();
                    detailData(data);
                    if(data != null){
                        BrandAdapter brandAdapter =  new BrandAdapter(dataList);
                        mRecyclerView.setAdapter(brandAdapter);
                        System.out.println();
                    }

                }catch (Exception e){

                }
            }
        });
    }
    ArrayList<Brand2Bean> dataList = new ArrayList<>();
    private void detailData(List<BrandBean.DataBean> data) {
        try {

            for (int i = 0; i < data.size(); i++) {
                BrandBean.DataBean dataBean = data.get(i);
                String other1 = dataBean.getOther1();
                if (TextUtils.isEmpty(other1))
                    return;

                List<BrandBean.DataBean> content = getContent(dataList, other1);
                if(content != null){
                    content.add(dataBean);
                }else{
                    Brand2Bean brand2Bean = new Brand2Bean();
                    content = new ArrayList<BrandBean.DataBean>();
                    content.add(dataBean);

                    brand2Bean.setOther1(other1);
                    brand2Bean.setData(content);
                    dataList.add(brand2Bean);
                }

            }

            System.out.println("");
        }catch (Exception e){

        }
    }

    private  List<BrandBean.DataBean> getContent(ArrayList<Brand2Bean> dataList, String key) {
        List<BrandBean.DataBean> data = null;
        try {
            if(dataList == null || dataList.size() == 0 || TextUtils.isEmpty(key))
                return data;

            for (int i = 0; i < dataList.size(); i++) {
                Brand2Bean brand2Bean = dataList.get(i);
                String other = brand2Bean.getOther1();
                if(key.equals(other)){
                    data = brand2Bean.getData();
                }
            }

        }catch (Exception e){

        }

        return data;

    }


    class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.ViewHolder>{


        private ArrayList<Brand2Bean> adapterList;

        public BrandAdapter(ArrayList<Brand2Bean> adapterList) {

            this.adapterList = adapterList;
        }

        @NonNull
        @Override
        public BrandAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.brand_item_new, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new BrandAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull BrandAdapter.ViewHolder holder, int position) {
            Brand2Bean brand2Bean = adapterList.get(position);

       /*     holder.ll_content.addView(new SingleBrandView(getActivity()));
            holder.ll_content.addView(new SingleBrandView(getActivity()));*/
           try {
               holder.tv_name.setText(brand2Bean.getOther1());
               List<BrandBean.DataBean> data = brand2Bean.getData();
               for (int i = 0; i < data.size(); i++) {
                   //开始
                   BrandBean.DataBean dataBean = data.get(i);
                   SingleBrandView singleBrandView = new SingleBrandView(getActivity());
                   singleBrandView.setData(dataBean);
                   holder.ll_content.addView(singleBrandView);
               }
           }catch (Exception e){

           }

           /* holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), BrandListActivity.class);
                    intent.putExtra("brandId",brand2Bean.);
                    startActivity();
                }
            });*/
        }

        @Override
        public int getItemCount() {
            return adapterList.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            LinearLayout ll_content;
            TextView tv_name;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                ll_content = itemView.findViewById(R.id.ll_content);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }

    @Override
    public void onLazyLoad() {


        //yc

//        getData();
    }









}
